package com.lsp.test.concurrentutils;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchExample  {

    static CountDownLatch countDownLatch = new CountDownLatch(3);

    static class Thread1 extends Thread{
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
        }
    }
    static class Thread2 extends Thread{
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
    }

    }
    static class Thread3 extends Thread{
        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            countDownLatch.countDown();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread1 t1 = new Thread1();
        Thread1 t2 = new Thread1();
        Thread1 t3 = new Thread1();
        t1.start();
        t2.start();
        t3.start();
//        countDownLatch.countDown();
//        countDownLatch.countDown();
//        countDownLatch.countDown();
        countDownLatch.await();
        System.out.println("所有线程执行结束！");
    }
}
